<?php
/**
 * @ClassName: UserLogic
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-10-09 17:23
 * @Version: V1.0.0
 */

namespace app\common\logic;

use app\common\enum\MerchantEnum;
use app\common\model;

class UserLogic
{
    /**
     * 验证用户手机号
     * @param $mobile
     * @param $role
     * @return bool
     */
    static function checkUserMobile($mobile, $role)
    {
        switch ($role) {
            case 1:
                // 商户
                $dataModel = new model\Merchant();
                break;
            case 2:
                // 门店
                $dataModel = new model\Store();
                break;
            case 3:
                // 代理
                $dataModel = new model\Agent();
                break;
            case 4:
                // 师傅
                $dataModel = new model\Master();
                break;
            case 5:
                // 员工
                $dataModel = new model\StoreStaff();
                break;
            default:
                // 普通会员
                $dataModel = new model\Member();
                break;
        }
        $user = $dataModel->where('username', $mobile)->find();

        if ($user) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 修改用户信息
     * @param $id
     * @param $params
     * @param $role
     * @return model\Agent|model\Master|model\Member|model\Merchant|model\Store|model\StoreStaff
     */
    static function updateUserInfo($id, $params, $role)
    {
        switch ($role) {
            case 1:
                // 商户
                $dataModel = new model\Merchant();
                if (isset($params['nickname'])) {
                    $params['contact_name'] = $params['nickname'];
                }
                unset($params['nickname']);
                if (isset($params['username'])) {
                    $params['contact_phone'] = $params['username'];
                }
                break;
            case 2:
                // 门店
                $dataModel = new model\Store();
                if (isset($params['nickname'])) {
                    $params['contact_name'] = $params['nickname'];
                }
                unset($params['nickname']);
                if (isset($params['username'])) {
                    $params['contact_phone'] = $params['username'];
                }
                break;
            case 3:
                // 代理
                $dataModel = new model\Agent();
                if (isset($params['nickname'])) {
                    $params['contact_name'] = $params['nickname'];
                }
                unset($params['nickname']);
                if (isset($params['username'])) {
                    $params['contact_phone'] = $params['username'];
                }
                break;
            case 4:
                // 师傅
                $dataModel = new model\Master();
                if (isset($params['nickname'])) {
                    $params['realname'] = $params['nickname'];
                }
                unset($params['nickname']);
                if (isset($params['username'])) {
                    $params['contact_phone'] = $params['username'];
                }
                break;
            case 5:
                // 员工
                $dataModel = new model\StoreStaff();
                if (isset($params['nickname'])) {
                    $params['realname'] = $params['nickname'];
                }
                unset($params['nickname']);
                if (isset($params['username'])) {
                    $params['contact_phone'] = $params['username'];
                }
                break;
            default:
                // 普通会员
                $dataModel = new model\Member();
                if (isset($params['username'])) {
                    $params['mobile'] = $params['username'];
                }
                break;
        }

        if (isset($params['password'])) {
            $user = $dataModel->get($id);
            if ($user) {
                $pass = passEncryp($user['salt'], $params['password']);
                $params['password'] = $pass;
            }
        }

        $params['update_time'] = date_now();

        return $dataModel->where('id', $id)->update($params);
    }

    /**
     * 获取登录门店信息
     * @param $role
     * @param $user
     * @return model\Store|array
     */
    static function loginStore($role, $user)
    {
        $user['user_type_txt'] = '';
        switch ($role) {
            case 1:
                // 商家登录
                $store = model\Store::where('merchant_id', $user['id'])->order('last_login_time desc,create_time desc')->find();
                $user['nickname'] = $user['contact_name'];
                $user['mobile'] = $user['contact_phone'];
                $user['user_type_txt'] = MerchantEnum::storeTypeText($user['user_type']);
                break;
            case 2:
                // 门店登录
                $store = $user;
                $user['nickname'] = $user['contact_name'];
                $user['mobile'] = $user['contact_phone'];
                break;
            case 3:
                // 代理登录
                $store = [];
                $user['nickname'] = $user['contact_name'];
                $user['mobile'] = $user['contact_phone'];
                break;
            case 4:
                // 师傅登录
                $store = model\Store::where('id', $user['store_id'])->order('last_login_time desc,create_time desc')->find();
                $user['nickname'] = $user['realname'];
                $user['mobile'] = $user['contact_phone'];
                break;
            case 5:
                // 员工登录
                $store = model\Store::where('id', $user['store_id'])->order('last_login_time desc,create_time desc')->find();
                $user['nickname'] = $user['realname'];
                $user['mobile'] = $user['contact_phone'];
                break;
            default:
                // 普通用户登录
                $store = [];
                break;
        }

        if ($store) {
            // 去除敏感信息
            unset($store['password'], $store['salt'], $store['super_pass'], $store['token']);
        }

        return [$store, $user];
    }

    /**
     *
     * @param $id
     * @param $role
     * @return model\Agent|model\Master|model\Member|model\Merchant|model\Store|model\StoreStaff
     */
    static function getUserMember($id, $role)
    {
        switch ($role) {
            case 1:
                // 商家登录
                $dataModel = new model\Merchant();
                $user_type = '商家';
                break;
            case 2:
                // 门店登录
                $dataModel = new model\Store();
                $user_type = '门店';
                break;
            case 3:
                // 代理登录
                $dataModel = new model\Agent();
                $user_type = '代理';
                break;
            case 4:
                // 师傅登录
                $dataModel = new model\Master();
                $user_type = '师傅';
                break;
            case 5:
                // 员工登录
                $dataModel = new model\StoreStaff();
                $user_type = '员工';
                break;
            default:
                // 普通用户登录
                $dataModel = new model\Member();
                $user_type = '会员';
                break;
        }

        $user = $dataModel::get($id);

        // 获取会员和代理信息
        $member = [];
        $agent = [];
        if (isset($user['member_id']) && $user['member_id']) {
            $member = model\Member::get($user['member_id']);
            $agent = model\Agent::where('member_id', $user['member_id'])->find();
            if ($member && $agent) {
                $member['agent_level_text'] = model\AgentLevel::where('level', $agent['agent_level'])->value('name') ?? 'J0';
                $member['attract_level_text'] = model\AgentAward::where('level', $agent['attract_level'])->where('type', 1)->value('name') ?? 'Z0';
                $member['operation_level_text'] = model\AgentAward::where('level', $agent['operation_level'])->where('type', 3)->value('name') ?? '';
            }
        }
        $user['member'] = $member;
        $user['agent'] = $agent;

        $user['avatar'] = replace_url($user['avatar']);

        return $user;
    }

}